package com.atguigu.gmall.common.intercetors;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.util.UserAuthUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO 在 new RequestTemplate -> new Request之间  会有拦截器的介入 RequestInterceptor
 * TODO 透传用户头信息  把老请求（网关）的用户id和 tempId请求中的东西放到新请求
 *  TODO 在同一个线程下拿数据
 */
@Component
public class UserHeaderFeignIntercetor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        //TODO 获取想要的请求
      //1获取老请求
        HttpServletRequest request = UserAuthUtil.request();
        String userId = request.getHeader(RedisConst.USER_ID_HEADER);
        if(!StringUtils.isEmpty(userId)){
            requestTemplate.header(RedisConst.USER_ID_HEADER,userId);
        }
        String tempId = request.getHeader(RedisConst.TEMP_ID_HEADER);
        if(!StringUtils.isEmpty(tempId)){
            //tempId传下去
            requestTemplate.header(RedisConst.TEMP_ID_HEADER,tempId);
        }

    }
}
